/*
 * @author : Amissah Gideon<kuulmek@yahoo.com>
 */
var Debtors = {

    selected_row_id:null,

    /* dataTable Object */
    objGrid:null,

    /* Function init
     * @param void
     * @return void
     * @access public
     * */
    init:function () {
        var self = this;

        /*Sortable Table */
        var oTable = null;
        var dataUrl = $('#table-url').val();

        self.objGrid = $('#flex').flexigrid({
            url:dataUrl,
            dataType:'json',
            colModel:[
                {display:'ID', name:'id', width:20, sortable:false, align:'left', hide:true},
                //{display:'Purchased Date', name:'purchased_date', width:100, sortable:true, align:'center', hide:false},
                {display:'Customer', name:'customer_id', width:170, sortable:false, align:'center', hide:false},
                {display:'Qty', name:'qty', width:20, sortable:false, align:'center', hide:false},
                {display:'Model', name:'items', width:150, sortable:false, align:'center', hide:false},
                {display:'Selling Price', name : 'cost', width : 80, sortable : false, align: 'center', hide:false},
                {display:'Deposit', name : 'deposit', width : 100, sortable : false, align: 'center', hide:false},
                {display:'Loan', name:'loan', width:60, sortable:false, align:'center', hide:false},
                {display:'Interest', name:'interest', width:100, sortable:false, align:'center', hide:false},
                {display:'Total', name:'total', width:100, sortable:false, align:'center', hide:false},
                {display:'Payments', name:'payments', width:80, sortable:false, align:'center', hide:false},
                {display:'Undue', name : 'undue', width : 80, sortable : false, align: 'center', hide:false},
                {display:'Past Due', name:'pastdue', width:100, sortable:false, align:'center', hide:false},
                {display:'Total Outstanding', name:'total_out', width:100, sortable:false, align:'center', hide:false},
                {display: 'Remarks', name : 'remarks', width : 100, sortable : false, align: 'center', hide:false}
            ],
            formFields:[
                {type:'buttom', name:'Print', bclass:'print', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Export', bclass:'export', onpress:self.handleGridEvent},
                {separator:true},
                {type:'select', name:'Select Currency', id:'select_currency' ,bclass:'person', options:gbl_currency_opts , onchange:self.handleGridEvent},
                {separator:true},
                {type:'select', name:'Filter On Customer', id:'select_customer' ,bclass:'person', options:gbl_customers_sales , onchange:self.handleGridEvent}
            ],
            /*searchitems : [
             {display: 'Name', name : 'name', isdefault: true}
             ],*/
            checkboxSelection:true,
            columnControl:true,
            sortname:"id",
            sortorder:"desc",
            usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            onSubmit: self.addFormData
        });

        /** Credit Section **/

        self.initDataTypeMenus();
    },

    handleGridEvent:function (com, grid, json) {
        if (com == 'More Detail') {
            if (jLib.rowSelectedCheck(grid, 1)) {
                Debtors.view(jLib.getSelectedRowIds(grid));
            }
        }
        else if (com == 'Print') {
            if (jLib.rowSelectedCheck(grid)) {
                Debtors.print_export_Details(jLib.getSelectedRowIds(grid),'print');
            }
            //Debtors.print_export_Details('print');
        }
        else if (com == 'Export') {
            if (jLib.rowSelectedCheck(grid)) {
                Debtors.print_export_Details(jLib.getSelectedRowIds(grid),'export');
            }
           // Debtors.print_export_Details('');
        }
        else if (com == 'Select Currency') {
            Debtors.filterCurrency(json);
        }
        else if (com == 'Filter On Customer') {
            Debtors.filterCustomers(json);
        }

    },

    filterCurrency:function(value){
        var self = this;
        self.objGrid.flexOptions({params: [{name:'currency',value:value}]}).flexReload();
        return false;
    },

    filterCustomers:function(value){
        var self = this;
        self.objGrid.flexOptions({params: [{name:'customer_id',value:value}]}).flexReload();
        return false;
    },

    addFormData:function(){
        var self = this;
        var val = $("#select_currency").val();
        var customer_id = $("#select_customer").val();
        //self.objGrid.flexOptions({params: [{name:'customer_id',value:val}]});
        $('#flex').flexOptions({params: [{name:'currency',value:val},{name:'customer_id',value:customer_id}]});
        return true;
    },

    print_export_Details: function(row_ids,data_type){
        var data_ids = '';
        if (typeof row_ids != null) {
            for (var x in row_ids) {
                data_ids += '' + row_ids[x] + ','
            }
        }
        $("#print-export-form #data_type").val(data_type);
        $("#print-export-form #data_ids").val(data_ids);
        $("#print-export-form #pe_currency").val($("#select_currency").val());
        $("#print-export-form #pe_customer_id").val($("#select_customer").val());
        window.open('', "PrintExportWindow", "menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
        //document.getElementById('popUpform').submit();
        $("#print-export-form").submit();

        //var dataUrl = $('#print-export-debtors-url').val();
       // var currency = $("#select_currency").val();
        //var url = dataUrl+'/'+currency+'/'+data_type;
        //window.open(url,data_type,"menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
    },

    initDataTypeMenus:function(){
        var self = this;

        $("button.data-type-menu").live('click',function(){
            var action = $(this).attr('action');
            var data_type = $(this).attr('data-type');
            var url = $("#"+action+"-url").val()+'/'+self.selected_row_id+'/'+data_type;

            window.open(url,action,"menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
        });

    }
};

/* when the page is loaded */
$(document).ready(function () {
    Debtors.init();
});